﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-13
Description:ServerInfo三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class ServerInfoService
	{
		#region 增加一条数据
		public static int AddServerInfo(ServerInfo serverInfo)
		{
			string sql =
			"INSERT INTO ServerInfo(SenderName,SenderID,ServerUrl,SenderPsw) "+
			"VALUES (@SenderName,@SenderID,@ServerUrl,@SenderPsw)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@SenderName",serverInfo.SenderName),
						
					new SQLiteParameter("@SenderID",serverInfo.SenderID),
						
					new SQLiteParameter("@ServerUrl",serverInfo.ServerUrl),
						
					new SQLiteParameter("@SenderPsw",serverInfo.SenderPsw)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteServerInfoById(long id)	
		{
			string sql="DELETE From  ServerInfo  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyServerInfoByid(ServerInfo serverInfo)
		{
		string sql ="UPDATE "+"ServerInfo"+" set "+
					"SenderName=@SenderName, "+
					"SenderID=@SenderID, "+
					"ServerUrl=@ServerUrl, "+
					"SenderPsw=@SenderPsw "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",serverInfo.Id),
					new SQLiteParameter("@SenderName",serverInfo.SenderName),
					new SQLiteParameter("@SenderID",serverInfo.SenderID),
					new SQLiteParameter("@ServerUrl",serverInfo.ServerUrl),
					new SQLiteParameter("@SenderPsw",serverInfo.SenderPsw)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<ServerInfo> GetAllServerInfos()
		{
			string sqlAll = "SELECT * FROM ServerInfo";
			return GetServerInfosBySql(sqlAll);
		}
		//以标识列获得对象
		public static ServerInfo GetServerInfoByid(object id)
		{
			string sql = "SELECT * FROM "+"ServerInfo"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					ServerInfo serverInfo = new ServerInfo();
					serverInfo.Id=Convert.ToInt32(reader["id"]);
					serverInfo.SenderName=Convert.ToString(reader["SenderName"]);
					serverInfo.SenderID=Convert.ToString(reader["SenderID"]);
					serverInfo.ServerUrl=Convert.ToString(reader["ServerUrl"]);
					serverInfo.SenderPsw=Convert.ToString(reader["SenderPsw"]);
					reader.Close();
					
					return serverInfo;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<ServerInfo> GetServerInfosBySql(string safeSql)
		{
			List<ServerInfo> list = new List<ServerInfo>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					ServerInfo serverInfo = new ServerInfo();
					serverInfo.Id=Convert.ToInt32(row["id"]);
					serverInfo.SenderName=Convert.ToString(row["SenderName"]);
					serverInfo.SenderID=Convert.ToString(row["SenderID"]);
					serverInfo.ServerUrl=Convert.ToString(row["ServerUrl"]);
					serverInfo.SenderPsw=Convert.ToString(row["SenderPsw"]);
					list.Add(serverInfo);
				}
				return list;
			}
             
		}
		private static IList<ServerInfo> GetServerInfosBySql( string sql, params SQLiteParameter[] values )
		{
			List<ServerInfo> list = new List<ServerInfo>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					ServerInfo serverInfo = new ServerInfo();
					serverInfo.Id=Convert.ToInt32(row["id"]);
					serverInfo.SenderName=Convert.ToString(row["SenderName"]);
					serverInfo.SenderID=Convert.ToString(row["SenderID"]);
					serverInfo.ServerUrl=Convert.ToString(row["ServerUrl"]);
					serverInfo.SenderPsw=Convert.ToString(row["SenderPsw"]);
					list.Add(serverInfo);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

